<template>
  <div class="register-container">
    <!-- 注册内容 -->
    <div class="register">
      <h3>
        注册新用户
        <span class="go"
          >我有账号，去
          <router-link href="javascript:;" to="/login"
            >登陆</router-link
          >
        </span>
      </h3>
      <!-- 手机号 -->
      <div class="content">
        <label>手机号:</label>
        <input
          name="phone"
          v-validate="{ required: true, regex: /^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/ }"
          type="text"
          placeholder="请输入你的手机号"
          v-model="phone" />
        <span class="error-msg">{{ errors.first('phone') }}</span>
      </div>
      <!-- 验证码 -->
      <div class="content">
        <label>验证码:</label>
        <input
          type="text"
          placeholder="请输入验证码"
          v-model="code"
          name="code"
          v-validate="{
            required: true,
            regex: /^\d{6}$/,
          }" />
        <el-button
          type="danger"
          size="mini"
          style="margin-left: 15px"
          @click="getcode"
          >获取验证码</el-button
        >
        <span class="error-msg">{{ errors.first('code') }}</span>
      </div>
      <!-- 密码 -->
      <div class="content">
        <label>登录密码:</label>
        <input
          type="text"
          placeholder="请输入你的登录密码"
          v-model="password"
          name="password"
          v-validate="{
            required: true,
            regex: /^(?=.*[a-zA-Z])(?=.*\d).{1,9}$/,
          }" />
        <span class="error-msg">{{ errors.first('password') }}</span>
      </div>
      <!-- 确认密码 -->
      <div class="content">
        <label>确认密码:</label>
        <input
          type="text"
          placeholder="请输入确认密码"
          v-model="password1"
          name="password1"
          v-validate="{ required: true, is: password }" />
        <span class="error-msg">{{ errors.first('password1') }}</span>
      </div>
      <!-- 同意协议 -->
      <div class="controls">
        <input
          type="checkbox"
          v-model="agree"
          name="agree"
          v-validate="{ required: true, agree: true }" />
        <span>同意协议并注册《尚品汇用户协议》</span>
        <span class="error-msg">{{ errors.first('argee') }}</span>
      </div>
      <!-- 点击注册 -->
      <div class="btn">
        <button @click="finishRegister">完成注册</button>
      </div>
    </div>

    <!-- 底部 -->
    <div class="copyright">
      <ul>
        <li><a href="javascript">关于我们</a></li>
        <li><a href="javascript">联系我们</a></li>
        <li><a href="javascript">联系客服</a></li>
        <li><a href="javascript">商家入驻</a></li>
        <li><a href="javascript">营销中心</a></li>
        <li><a href="javascript">手机尚品汇</a></li>
        <li><a href="javascript">销售联盟</a></li>
        <li><a href="javascript">尚品汇社区</a></li>
      </ul>
      <div class="address">
        地址：北京市昌平区宏福科技园综合楼6层
      </div>
      <div class="beian">京ICP备19006430号</div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data() {
    return {
      // 收集用户注册的手机号
      phone: '',
      // 存储用户获取到的验证码
      code: '',
      //收集用户的密码
      password: '',
      // 确认密码
      password1: '',
      // 同意协议
      agree: true,
    };
  },
  methods: {
    // 获取验证码的回调
    async getcode() {
      let { phone } = this;
      // 判断输入的文本不能为空
      if (phone.trim() != '') {
        // 通知vuex发请求(携带参数:用户的手机号)
        try {
          await this.$store.dispatch('getusercode', phone);
          // 将vuex中的验证码赋值给组件中data的code数据
          this.code = this.$store.state.User.code;
        } catch (error) {}
      }
    },
    // 注册的回调
    finishRegister() {
      let { phone, password, password1, code, agree } =
        this;
      if (
        phone.trim() != '' &&
        password === password1 &&
        agree
      ) {
        // 通知vuex发请求(携带参数:手机号,验证码,密码)
        this.$store
          .dispatch('userRegister', {
            phone,
            password,
            code,
          })
          .then(
            (res) => {
              // 注册成功之后要做的事(跳转到登录页面)
              this.$router.push({ name: 'login' });
            },
            (error) => {
              // 失败之后要做的事
              this.$message({
                message: '注册失败',
                type: 'warning',
              });
            }
          );
      }
    },
  },
};
</script>

<style scoped lang="less">
.register-container {
  .register {
    width: 1200px;
    height: 445px;
    border: 1px solid rgb(223, 223, 223);
    margin: 0 auto;

    h3 {
      background: #ececec;
      margin: 0;
      padding: 6px 15px;
      color: #333;
      border-bottom: 1px solid #dfdfdf;
      font-size: 20.04px;
      line-height: 30.06px;

      span {
        font-size: 14px;
        float: right;

        a {
          color: #e1251b;
        }
      }
    }

    div:nth-of-type(1) {
      margin-top: 40px;
    }

    .content {
      padding-left: 390px;
      margin-bottom: 18px;
      position: relative;

      label {
        font-size: 14px;
        width: 96px;
        text-align: right;
        display: inline-block;
      }

      input {
        width: 270px;
        height: 38px;
        padding-left: 8px;
        box-sizing: border-box;
        margin-left: 5px;
        outline: none;
        border: 1px solid #999;
      }

      img {
        vertical-align: sub;
      }

      .error-msg {
        position: absolute;
        top: 100%;
        left: 495px;
        color: red;
      }
    }

    .controls {
      text-align: center;
      position: relative;

      input {
        vertical-align: middle;
      }

      .error-msg {
        position: absolute;
        top: 100%;
        left: 495px;
        color: red;
      }
    }

    .btn {
      text-align: center;
      line-height: 36px;
      margin: 17px 0 0 55px;

      button {
        outline: none;
        width: 270px;
        height: 36px;
        background: #e1251b;
        color: #fff !important;
        display: inline-block;
        font-size: 16px;
      }
    }
  }

  .copyright {
    width: 1200px;
    margin: 0 auto;
    text-align: center;
    line-height: 24px;

    ul {
      li {
        display: inline-block;
        border-right: 1px solid #e4e4e4;
        padding: 0 20px;
        margin: 15px 0;
      }
    }
  }
}
</style>
